extends Card

@export var optional_sound: AudioStream


func get_default_tooltip() -> String:
	return tooltip_text


func get_updated_tooltip(_player_modifiers: ModifierHandler, _enemy_modifiers: ModifierHandler) -> String: # step 7.1
	return tooltip_text


func apply_effects(targets: Array[Node], _modifiers: ModifierHandler) -> void:
	print("🎯 九重灾劫术卡牌被使用！")
	print("🎯 目标数量: %d" % targets.size())
	
	# 播放卡牌使用音效
	if sound:
		SFXPlayer.play(sound)
	elif optional_sound:
		SFXPlayer.play(optional_sound)
	
	# 获取battle引用
	var battle = _get_battle(targets)
	if not battle:
		print("❌ 无法获取battle引用")
		return
	
	if not battle.resource_scheduler:
		print("❌ 无法获取resource_scheduler")
		return
	
	print("✅ 成功获取battle和resource_scheduler")
	
	# 获取当前回合数
	var current_turn = battle.current_turn
	print("📊 九重灾劫术记录回合数: %d" % current_turn)
	print("📊 当前阶段: %d" % battle.current_phase)
	
	# 创建两个调度效果：一个记录伤害，一个给予护甲
	var record_effect = _create_damage_record_effect(current_turn)
	var block_effect = _create_block_grant_effect(current_turn)
	
	# 将调度效果添加到资源调度器
	battle.resource_scheduler.add_scheduled_effect(record_effect)
	battle.resource_scheduler.add_scheduled_effect(block_effect)
	
	print("🔗 九重灾劫术调度效果已添加到资源调度器")
	print("🔗 调度器当前效果数量: %d" % battle.resource_scheduler.scheduled_effects.size())
	print("🔗 记录效果ID: %s" % record_effect.id)
	print("🔗 护甲效果ID: %s" % block_effect.id)
	print("Targets: %s" % targets)


# 创建伤害记录调度效果
func _create_damage_record_effect(current_turn: int) -> ScheduledEffect:
	print("🔧 _create_damage_record_effect: 开始创建伤害记录效果，当前回合=%d" % current_turn)
	
	var effect = ScheduledEffect.new()
	effect.id = "ninefold_calamity_record_%d" % current_turn
	effect.trigger_type = ScheduledEffect.TriggerType.PHASE_SPECIFIC
	effect.target_type = ScheduledEffect.TargetType.PLAYER
	effect.trigger_turn = current_turn  # 当前回合
	effect.trigger_phase = TurnPhaseConstants.ENEMY_END  # 敌人回合结束阶段
	effect.is_repeating = false
	effect.max_triggers = 1  # 只触发一次
	
	print("🔧 伤害记录效果基础配置完成: ID=%s, 触发类型=%d, 目标类型=%d" % [effect.id, effect.trigger_type, effect.target_type])
	print("🔧 触发条件: 回合=%d, 阶段=%d, 重复=%s, 最大触发=%d" % [effect.trigger_turn, effect.trigger_phase, effect.is_repeating, effect.max_triggers])
	
	# 创建通用伤害记录效果
	var damage_recorder = DamageRecorderEffect.new()
	damage_recorder.configure("ninefold_calamity", current_turn)  # 使用卡牌名称作为记录键
	
	print("🔧 通用伤害记录效果创建完成: 记录键=%s, 回合=%d" % [damage_recorder.record_key, damage_recorder.turn_used])
	
	# 将自定义效果添加到调度效果
	effect.custom_effects.append(damage_recorder)
	
	print("🔧 自定义效果已添加到调度效果，custom_effects数量: %d" % effect.custom_effects.size())
	print("🔧 创建伤害记录效果: %s (回合 %d, 阶段 %d)" % [effect.id, effect.trigger_turn, effect.trigger_phase])
	
	return effect


# 创建护甲给予调度效果
func _create_block_grant_effect(current_turn: int) -> ScheduledEffect:
	print("🔧 _create_block_grant_effect: 开始创建护甲给予效果，当前回合=%d" % current_turn)
	
	var effect = ScheduledEffect.new()
	effect.id = "ninefold_calamity_block_%d" % current_turn
	effect.trigger_type = ScheduledEffect.TriggerType.PHASE_SPECIFIC
	effect.target_type = ScheduledEffect.TargetType.PLAYER
	effect.trigger_turn = current_turn + 1  # 下一回合
	effect.trigger_phase = TurnPhaseConstants.PLAYER_MAIN  # 玩家主要阶段
	effect.is_repeating = false
	effect.max_triggers = 1  # 只触发一次
	
	print("🔧 护甲给予效果基础配置完成: ID=%s, 触发类型=%d, 目标类型=%d" % [effect.id, effect.trigger_type, effect.target_type])
	print("🔧 触发条件: 回合=%d, 阶段=%d, 重复=%s, 最大触发=%d" % [effect.trigger_turn, effect.trigger_phase, effect.is_repeating, effect.max_triggers])
	
	# 创建通用护甲给予效果
	var block_granter = BlockGranterEffect.new()
	block_granter.configure("ninefold_calamity", current_turn, 1.0)  # 1:1护甲比例
	
	print("🔧 通用护甲给予效果创建完成: 记录键=%s, 回合=%d, 倍数=%.1f" % [block_granter.record_key, block_granter.turn_used, block_granter.block_multiplier])
	
	# 将自定义效果添加到调度效果
	effect.custom_effects.append(block_granter)
	
	print("🔧 自定义效果已添加到调度效果，custom_effects数量: %d" % effect.custom_effects.size())
	print("🔧 创建护甲给予效果: %s (回合 %d, 阶段 %d)" % [effect.id, effect.trigger_turn, effect.trigger_phase])
	
	return effect


# 获取战斗场景 - 通过targets参数获取场景树
func _get_battle(targets: Array[Node]) -> Node:
	print("🔍 _get_battle: 目标数量=%d" % targets.size())
	
	if targets.size() == 0:
		print("❌ _get_battle: 没有目标")
		return null
	
	var first_target = targets[0]
	print("🔍 _get_battle: 第一个目标类型=%s" % first_target.get_class())
	
	var tree = first_target.get_tree()
	if not tree:
		print("❌ _get_battle: 无法获取场景树")
		return null
	
	print("✅ _get_battle: 成功获取场景树")
	
	var battle = Card.find_battle(tree)
	if battle:
		print("✅ _get_battle: 成功找到battle节点")
	else:
		print("❌ _get_battle: 未找到battle节点")
	
	return battle


# 获取玩家 - 通过targets参数获取场景树
func _get_player(targets: Array[Node]) -> Node:
	if targets.size() == 0:
		return null
	
	var tree = targets[0].get_tree()
	if not tree:
		return null
	
	return Card.find_player(tree)
